package com.hzcc.hzccblog.exception;


import com.hzcc.hzccblog.common.Result;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author 早睡蛋
 * @Date 2022/6/9 11:37:16
 * @Desc:
 */
@Controller
public class UnifiedExceptionHandler extends AbstractErrorController {


    public UnifiedExceptionHandler(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @RequestMapping("/error")
    @ResponseBody
    public Result error(HttpServletRequest request, HttpServletResponse response){
        response.setStatus(HttpStatus.OK.value());
        HttpStatus httpStatus = getStatus(request);
        Result resultResponse = Result.defaultResultResponse();
        resultResponse.setCode(httpStatus.value());
        resultResponse.setMessage(httpStatus.getReasonPhrase());
        return resultResponse;
    }

    @Override
    public String getErrorPath() {
        return "error";
    }
}
